﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Data;

namespace Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class mainWindow : Window
    {
        System.Windows.Threading.DispatcherTimer dispatcherTimer;
        public mainWindow()
        {
            InitializeComponent();
        }


        // USER MANAGEMENT

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Stop();
            dispatcherTimer.Start();
            if (tbxAccountName.Text != "")
            {
                formController.Login(tbxAccountName.Text, tbxPasswrod.Text);
            }
            else
            {
                MessageBox.Show("Please enter your account name");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnNewAccount_Click(object sender, RoutedEventArgs e)
        {
            formController.ShowForm(typeof(newUser));
        }

        private void btnLostAccount_Click(object sender, RoutedEventArgs e)
        {

        }


        // FORM MANAGEMENT

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (formController.newProtocol == true)
            {
                switch (formController.receivedProtocol)
                {

                    #region Login Result

                    case "LR":
                        {
                            if (formController.receivedMessage == "OK")
                            {
                                formController.ShowForm(typeof(saunaMain));
                                dispatcherTimer.Stop();
                            }
                            else
                            {
                                dispatcherTimer.Stop();
                                if (formController.errorMessage != "") { MessageBox.Show(formController.errorMessage); }
                            }
                        }
                        break;

                    #endregion

                    #region User Created

                    case "UC":
                        {
                            if (formController.receivedMessage == "OK")
                            {
                                formController.ShowForm(typeof(saunaMain));
                                dispatcherTimer.Stop();
                            }
                            else
                            {
                                dispatcherTimer.Stop();
                            }
                        }
                        break;

                    #endregion

                    #region User Exists

                    case "UE":
                        {

                        }
                        break;

                    #endregion

                    default:

                        break;
                }

                formController.newProtocol = false;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

            //dispatcherTimer.Start();
        }
    }
}
